﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TaoSH.AppCode;
using TaoSH.Data;

namespace TaoSH.View
{
    public partial class MangerBranch : BasePage
    {
        public string strAction
        {
            get
            {
                return Request.QueryString["Action"].ToString();
            }
        }

        public string strPBanchCode
        {
            get
            {
                return Request.QueryString["PID"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialData();
            }
        }

        protected void btAddBranch_Click(object sender, EventArgs e)
        {
            if (ChenckData())
            {
                if (strAction == "add")
                {
                    JH_Auth_Branch NewBranch = new JH_Auth_Branch();
                    NewBranch.DeptName = tbBranchName.Text;
                    NewBranch.DeptDesc = tbBranchDec.Text;
                    NewBranch.DeptRoot = int.Parse(strPBanchCode);
                    NewBranch.DeptShort = 0;
                    NewBranch.Remark1 = conFZR.Text;
                    new JH_Auth_BranchB().Insert(NewBranch);
                    AlertMSG("2", "添加部门成功！");
                }
                else
                {
                    JH_Auth_Branch BranchUP = new JH_Auth_BranchB().GetEntities(d => d.DeptCode == int.Parse(strPBanchCode)).SingleOrDefault();
                    BranchUP.DeptName = tbBranchName.Text;
                    BranchUP.DeptDesc = tbBranchDec.Text;
                    BranchUP.DeptRoot = int.Parse(ddlBranch.SelectedValue);
                    BranchUP.Remark1 = conFZR.Text;

                    new JH_Auth_BranchB().Update(BranchUP);
                    AlertMSG("2", "修改部门成功！");
                }
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// 服务器端验证
        /// </summary>
        /// <returns></returns>
        public bool ChenckData()
        {
            bool boolcheck = true;
            if (tbBranchName.Text.ToString() == "")
            {
                AlertMSG("1", "部门名称不能为空！");
                boolcheck = false;
            }
            return boolcheck;
        }

        public void InitialData()
        {
             new CommonHelp.BindData().BindBranch(ddlBranch,"Y");
            ddlBranch.Items.Insert(0, new System.Web.UI.WebControls.ListItem("请选择", "-1"));


            if (strAction != "add")
            {
                JH_Auth_Branch Branch = new JH_Auth_BranchB().GetEntities(d => d.DeptCode == int.Parse(strPBanchCode)).SingleOrDefault();
                tbBranchName.Text = Branch.DeptName;
                tbBranchDec.Text = Branch.DeptDesc;
                ddlBranch.SelectedValue = Branch.DeptRoot.ToString();
                conFZR.Text = Branch.Remark1;
            }
            else
            {
                ddlBranch.SelectedValue = strPBanchCode;

            }
        }
    }
}